package top.hekun.study;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.Tables;
import com.deepoove.poi.data.style.BorderStyle;
import com.deepoove.poi.data.style.TableStyle;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;

/**
 * @author:guoshuai
 * @date:2022/12/22 11:25
 * @description: 根据word模板生成报告demo
 * 说明：需要引入poi依赖jar包和commons-lang3的依赖包
 */
public class GenerateReport {
	public static void main(String[] args) {
		//基础数据
		Map<String,Object> dataMap = getDataMap();
		//报告模板位置
		File docTemplatePath =getFilePath("template.docx");
		//替换模板文件的占位符，生成目标报告文件
		replacePlaceholderWord(dataMap,docTemplatePath);
		System.out.println("生成报告完成");
	}

	private static File getFilePath(String name){

		File filePath = new File(new File(new File(new File(new File(System.getProperty("user.dir"), "poi-tl"), "src"), "main"), "resources"), name);
		return filePath;
	}

	private static void replacePlaceholderWord(Map<String, Object> dataMap, File docTemplatePath) {
		try{
			File docPath = getFilePath("result.docx");
			LoopRowTableRenderPolicy  policy = new LoopRowTableRenderPolicy();
			Configure config = Configure.builder().bind("opinion", policy).build();
			InputStream inputStream = new FileInputStream(docTemplatePath);

			XWPFTemplate template = XWPFTemplate.compile(inputStream, config).render(dataMap);


			template.writeAndClose(new FileOutputStream(docPath));
		}catch (Exception e){
			e.printStackTrace();
		}
	}

	//基础数据-样例
	private static Map<String, Object> getDataMap() {
		Map<String,Object> dataMap = new HashMap<>();
		dataMap.put("sqrid","20221222304");
		dataMap.put("id","非密");
		dataMap.put("sqbm","创新中心");
		dataMap.put("sqr","张三");
		dataMap.put("cbr","李四");
		dataMap.put("lxdh","12315");
		dataMap.put("bmwyksprid","2022-12-22");
		dataMap.put("tm","导出报告demo");
		dataMap.put("fileIds","1.测试附件.docx-非密");
		dataMap.put("nrsjfw","单位内部信息");
		dataMap.put("lb","其他");
		dataMap.put("fbxs","抖音");
		dataMap.put("fbfw","互联网");
		dataMap.put("zdfs","打印");
		dataMap.put("ymbz","这是一条备注");
		dataMap.put("ymbz","这是一条备注");
		List<Map<String,String>> opinionList = new ArrayList<>();
		Map<String,String> opinion1 = new HashMap<>();
		opinion1.put("taskName","申请部门意见");
		opinion1.put("optext","申请部门同意");
		Map<String,String> opinion2 = new HashMap<>();
		opinion2.put("taskName","审批部门意见");
		opinion2.put("optext","审批部门同意");
		opinionList.add(opinion1);
		opinionList.add(opinion2);

		dataMap.put("opinion",opinionList);
		List<Map<String,Object>> person = new ArrayList<>();

		Map<String,Object> subTable0 = new HashMap<>();
		subTable0.put("subTable",
				Tables.of(new String[][] {
				new String[] { "00", "01" },
				new String[] { "10", "11" }
		}).border(BorderStyle.DEFAULT).create()
		);
		Map<String,Object> subTable1 = new HashMap<>();
		subTable1.put("subTable", Tables.of(new String[][] {
				new String[] { "100", "101" },
				new String[] { "110", "111" }
		}).border(BorderStyle.DEFAULT).create());

		person.add(subTable0);
		person.add(subTable1);
		dataMap.put("person",person);


		return dataMap;
	}
}
